/**
 * 
 */
package com.test.mywebapp.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Image;

/**
 * @author I030107
 * The class represents a Cell on the Board for a game.
 */
public class BoardCell{
	/*
	 * Constants for the images to be used in the cell.
	 */
	final public static String ImgUrlBack 		= "mywebapp/gwt/standard/images/Back.jpg";
	final public static String ImgUrlPreCross	= "mywebapp/gwt/standard/images/PreCross.jpg";
	final public static String ImgUrlPreZero 	= "mywebapp/gwt/standard/images/PreZero.jpg";
	final public static String ImgUrlCross 		= "mywebapp/gwt/standard/images/Cross.jpg";
	final public static String ImgUrlZero  		= "mywebapp/gwt/standard/images/Zero.jpg";
	
	/*
	 * Indicates the image to be displayed in the cell.
	 */
	private Image cellImage = new Image();

	/*
	 * Indicates if the cell is currently occupied.
	 */
	private boolean occupied = false;
	
	/*
	 * Indicates the board on which the cell is placed.
	 */
	private Board board = null;
	
	/*
	 * Constructor for a Cell on the Board.
	 */
	public BoardCell(Board aBoard){
		this.board = aBoard;
		this.reset();
		CellEventHandler handler = new CellEventHandler(this);
		this.cellImage.addClickHandler(handler);
		this.cellImage.addMouseOverHandler(handler);
		this.cellImage.addMouseOutHandler(handler);
	}
	
	/*
	 * Gets the board of the cell.
	 */
	public Board getBoard(){
		return this.board;
	}

	/*
	 * Gets the given image to the cell.
	 */
	public Image getImage(){
		return this.cellImage;
	}

	/*
	 * Sets the given image to the cell, if it is unoccupied.
	 */
	public void setImage(String aUrl){
		if (!this.occupied){
			this.cellImage.setUrl(aUrl);
		}
	}
	
	/*
	 * Makes the cell a virgin.
	 */
	public void reset(){
		this.occupied = false;
		this.cellImage.setUrl(BoardCell.ImgUrlBack);
	}
	
	/*
	 * Makes the cell a virgin.
	 */
	public void refresh(){
		if (!this.occupied){
			this.cellImage.setUrl(BoardCell.ImgUrlBack);
		}
	}
	
	/*
	 * Sets the given image to the cell and set it to occupied.
	 */
	public void occupy(){
		this.occupied = true;
		if (this.board.player() == Board.Cross){
			this.cellImage.setUrl(BoardCell.ImgUrlCross);
		}
		else{
			this.cellImage.setUrl(BoardCell.ImgUrlZero);			
		}
		this.board.occupy();
	}
		
	/*
	 * Sets the given image to the cell and set it to occupied.
	 */
	public void preview(){
		if (!this.occupied){
			if (this.board.player() == Board.Cross){
				this.cellImage.setUrl(BoardCell.ImgUrlPreCross);
			}
			else{
				this.cellImage.setUrl(BoardCell.ImgUrlPreZero);			
			}
		}
	}
	
	/*
	 * Returns the occupied state of the cell.
	 */
	public boolean isOccupied(){
		return this.occupied;
	}

	/**
	 * Creates an event handler for the Cell.
	 */
	private class CellEventHandler implements ClickHandler, MouseOverHandler, MouseOutHandler
	{
		/*
		 * Indicates the cell.
		 */
		private BoardCell cell = null;
		
		CellEventHandler(BoardCell aCell){
			this.cell = aCell;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			this.cell.occupy();
		}

		@Override
		public void onMouseOver(MouseOverEvent event) {
			this.cell.preview();
		}

		@Override
		public void onMouseOut(MouseOutEvent event) {
			this.cell.refresh();		
		}
	}
}
